#ifndef TEST_LIST_INL
#define TEST_LIST_INL

#include "list.h"
#include "console.h"

void test_list() {
    list_t li;
    list_node_t a, b, c;

    list_init(&li);
    list_node_init(&a);
    list_node_init(&b);
    list_node_init(&c);

    // test list_push_back & list_pop_front
    list_push_back(&li, &a);
    list_push_back(&li, &b);
    list_push_back(&li, &c);

    assert(li.head == &a);
    assert(li.tail == &c);
    assert(a.prev == 0);
    assert(a.next == &b);
    assert(b.prev == &a);
    assert(b.next == &c);
    assert(c.prev == &b);
    assert(c.next == 0);

    list_pop_front(&li);
    assert(li.head == &b);
    assert(li.tail == &c);
    list_pop_front(&li);
    assert(li.head == &c);
    assert(li.tail == &c);
    list_pop_front(&li);
    assert(li.head == 0);
    assert(li.tail == 0);

    assert(a.prev == 0);
    assert(a.next == 0);
    assert(b.prev == 0);
    assert(b.next == 0);
    assert(c.prev == 0);
    assert(c.next == 0);

    // test list_push_front & list_pop_back
    list_push_front(&li, &a);
    assert(li.head == &a);
    assert(li.tail == &a);
    list_push_front(&li, &b);
    assert(li.head == &b);
    assert(li.tail == &a);
    list_push_front(&li, &c);
    assert(li.head == &c);
    assert(li.tail == &a);

    assert(li.head == &c);
    assert(li.tail == &a);
    assert(c.prev == 0);
    assert(c.next == &b);
    assert(b.prev == &c);
    assert(b.next == &a);
    assert(a.prev == &b);
    assert(a.next == 0);

    list_pop_back(&li);
    assert(li.head == &c);
    assert(li.tail == &b);
    list_pop_back(&li);
    assert(li.head == &c);
    assert(li.tail == &c);
    list_pop_back(&li);
    assert(li.head == 0);
    assert(li.tail == 0);

    assert(a.prev == 0);
    assert(a.next == 0);
    assert(b.prev == 0);
    assert(b.next == 0);
    assert(c.prev == 0);
    assert(c.next == 0);

    // test list_remove
    list_push_back(&li, &a);
    list_push_back(&li, &b);
    list_push_back(&li, &c);

    list_remove(&li, &b);
    assert(a.prev == 0);
    assert(a.next == &c);
    assert(b.prev == 0);
    assert(b.next == 0);
    assert(c.prev == &a);
    assert(c.next == 0);
    assert(li.head == &a);
    assert(li.tail == &c);

    list_remove(&li, &c);
    assert(a.prev == 0);
    assert(a.next == 0);
    assert(b.prev == 0);
    assert(b.next == 0);
    assert(c.prev == 0);
    assert(c.next == 0);
    assert(li.head == &a);
    assert(li.tail == &a);

    list_remove(&li, &a);
    assert(a.prev == 0);
    assert(a.next == 0);
    assert(b.prev == 0);
    assert(b.next == 0);
    assert(c.prev == 0);
    assert(c.next == 0);
    assert(li.head == 0);
    assert(li.tail == 0);
}

#endif